0.1.6Updated a month ago
import State from "@infinity-beyond/modules/state.ts";
import { plural } from "https://deno.land/x/deno_plural@2.0.0/mod.ts";
import type { Handlers, PageProps } from "$fresh/server.ts";

import { Ledger } from "@ledger/mod.ts";
import { Supply } from "@supply/supply.ts";
import type { I_Pagination } from "@infinity-beyond/modules/pagination.ts";
import { LedgerUserData } from "@ledger/ui/ledger.user.data.tsx";
import { LedgerUser } from "@ledger/ui/LedgerUser.tsx";

export const handler: Handlers<I_EntityUserScreen> = {
  async GET(request, ctx) {
    const entity_slug = ctx.params.slug;
    const key = ctx.params.key;

    let type: 'none' | 'audience' | 'ledger' | 'supply' = 'none';
    let entity;

    for(const [_name, entity_item] of State.Entities.entries()) {
      if(entity_slug !== plural(entity_item.slug)) continue;

      if(entity_item instanceof Ledger) {
        type = 'ledger';
      }
      if(entity_item instanceof Supply) {
        type = 'supply';
      }

      entity = entity_item as Entity.Entity<any, any>;
    }

    if(!entity || type === 'none') return ctx.renderNotFound();

    const data = await LedgerUserData(request, entity as Ledger.Ledger<any>, key);

    return ctx.render({ ...data, key, entity });
  }
}

interface I_EntityUserScreen {
  key: string,
  balance: number,
  entity: Entity.Entity<any>,
  entries: any[],
  meta: I_Pagination,
}

export default function EntityScreen({ data: { key, balance, entity, entries, meta }}: PageProps<I_EntityUserScreen>) {
  if(entity instanceof Ledger) {
    return (
      <LedgerUser user_key={key} balance={balance} ledger={entity} entries={entries} total_entries={meta.total_items} meta={meta} row_config={{ show_pagination: true }} />
    )
  }
}